/**
 * 基础节点组件
 * 所有工作流节点的基类
 */
export const BaseNode = {
  props: {
    nodeData: {
      type: Object,
      required: true
    },
    graph: {
      type: Object,
      required: true
    }
  },
  
  data() {
    return {
      isSelected: false,
      isHovered: false
    }
  },
  
  computed: {
    /**
     * 获取节点配置
     */
    nodeConfig() {
      return this.nodeData.data || {}
    },
    
    /**
     * 获取节点样式
     */
    nodeStyle() {
      return {
        cursor: 'pointer',
        border: this.isSelected ? '2px solid #1890ff' : '1px solid #d9d9d9',
        backgroundColor: this.isHovered ? '#f0f7ff' : '#ffffff',
        borderRadius: '6px',
        padding: '8px 12px',
        boxShadow: this.isSelected ? '0 0 8px rgba(24, 144, 255, 0.3)' : '0 2px 8px rgba(0, 0, 0, 0.1)',
        transition: 'all 0.2s ease'
      }
    }
  },
  
  methods: {
    /**
     * 处理节点点击事件
     */
    handleClick() {
      this.$emit('node-click', this.nodeData)
    },
    
    /**
     * 处理鼠标移入事件
     */
    handleMouseEnter() {
      this.isHovered = true
      this.$emit('node-mouseenter', this.nodeData)
    },
    
    /**
     * 处理鼠标移出事件
     */
    handleMouseLeave() {
      this.isHovered = false
      this.$emit('node-mouseleave', this.nodeData)
    },
    
    /**
     * 更新节点数据
     * @param {Object} newData - 新数据
     */
    updateNodeData(newData) {
      this.$emit('update-node', this.nodeData, newData)
    },
    
    /**
     * 验证节点数据
     * @returns {Array} 验证错误信息
     */
    validateNode() {
      const errors = []
      const config = this.nodeConfig
      
      if (!config.name || config.name.trim() === '') {
        errors.push('节点名称不能为空')
      }
      
      if (!config.code || config.code.trim() === '') {
        errors.push('节点编码不能为空')
      }
      
      return errors
    }
  },
  
  render() {
    return this.$slots.default ? this.$slots.default({
      nodeData: this.nodeData,
      nodeConfig: this.nodeConfig,
      nodeStyle: this.nodeStyle,
      isSelected: this.isSelected,
      isHovered: this.isHovered,
      handleClick: this.handleClick,
      handleMouseEnter: this.handleMouseEnter,
      handleMouseLeave: this.handleMouseLeave
    }) : null
  }
}

export default BaseNode